package com.hyw.trace.configuration.meta;

import java.util.Map;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
 * 定义 一个通用的收集事件, 作为采集方和收集方的流通载体
 * 
 * @author hongyu
 */
@Validated
public class TraceEvent {

  /**
   * 考虑到事件类型在不同的业务中, 有可能会出现命名相同,故引入一个分组的概念。
   * <p>
   * 允许空串，就相当于归入到同一个组中
   */
  @NotNull
  protected String group;

  /**
   * 事件类型，不允许为空，也不支持空串
   */
  @NotEmpty
  protected String type;

  /**
   * 针对该事件的描述
   */
  protected String description;

  /**
   * 用于表述该类型的相关属性集合,必须有值
   */
  @NotEmpty
  protected Map<String, ?> properties;

  @JsonCreator
  public TraceEvent(@JsonProperty("group") String group, @JsonProperty("type") String type,
      @JsonProperty("description") String description, @JsonProperty("properties") Map<String, ?> properties) {
    super();
    Assert.isTrue(StringUtils.hasText(group), "group 不允许为空");
    Assert.isTrue(StringUtils.hasText(type), "type 不允许为空");
    this.group = group;
    this.type = type;
    this.description = description;
    this.properties = properties;
  }

  public String getGroup() {
    return group;
  }

  public void setGroup(String group) {
    this.group = group;
  }

  public String getType() {
    return type;
  }

  public void setType(String type) {
    this.type = type;
  }

  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  public Map<String, ?> getProperties() {
    return properties;
  }

  public void setProperties(Map<String, ?> properties) {
    this.properties = properties;
  }

  @Override
  public String toString() {
    return "TraceEvent [group=" + group + ", type=" + type + ", description=" + description + ", properties="
        + properties + "]";
  }

}
